package com.yanqu.road.dao.impl.activity.dafuweng;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossDFWUnionDataDaoImpl extends TempDataObjectDao<DFWUnionData> {

    public static String getDFWUnionDataSql = "select * from t_c_dfw_union_data where activityId=? and groupId=?";

    public Map<String, DFWUnionData> getDFWUnionDataMap(int activityId, int groupId) {
        Map<String, DFWUnionData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDFWUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWUnionData dFWUnionData = parse(rs);
                map.put(dFWUnionData.getUnionUid(), dFWUnionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDFWUnionDataSql = "update t_c_dfw_union_data set buyTimesMap=?,rebateUserList=?,collectCountMap=?,addition=?,additionRecoveryTime=?,lastResetTime = ? where activityId=? and groupId=? and unionUid=?";

    @Override
    protected boolean updateData(DFWUnionData dFWUnionData) {
        dFWUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getBuyTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getRebateUserList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getCollectCountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getAddition()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionData.getAdditionRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUnionData.getUnionUid()));
        if (getDb().executeNoneQuery(updateDFWUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDFWUnionDataSql = "insert into t_c_dfw_union_data(unionUid,activityId,groupId,buyTimesMap,rebateUserList,collectCountMap,addition,additionRecoveryTime,lastResetTime) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DFWUnionData dFWUnionData) {
        dFWUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getBuyTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getRebateUserList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionData.getCollectCountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionData.getAddition()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionData.getAdditionRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionData.getLastResetTime()));
        if (getDb().executeNoneQuery(insertDFWUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DFWUnionData parseData(ResultSet rs) throws SQLException {
        DFWUnionData dFWUnionData = new DFWUnionData();
        dFWUnionData.setUnionUid(rs.getString("unionUid"));
        dFWUnionData.setActivityId(rs.getInt("activityId"));
        dFWUnionData.setGroupId(rs.getInt("groupId"));
        dFWUnionData.setBuyTimesMap(JSONObject.parseObject(rs.getString("buyTimesMap"), new TypeReference<Map<Long, Integer>>() {
        }));
        dFWUnionData.setRebateUserList(JSONObject.parseObject(rs.getString("rebateUserList"), new TypeReference<Map<Integer, List<Long>>>() {
        }));
        dFWUnionData.setCollectCountMap(JSONObject.parseObject(rs.getString("collectCountMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        dFWUnionData.setAddition(rs.getInt("addition"));
        dFWUnionData.setAdditionRecoveryTime(rs.getLong("additionRecoveryTime"));
        dFWUnionData.setLastResetTime(rs.getLong("lastResetTime"));
        return dFWUnionData;
    }


}
